{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "def gcd(pair):\n",
    "    a, b = pair\n",
    "    low = min(a, b)\n",
    "    for i in range(low, 0, -1):\n",
    "        if a % i == 0 and b % i == 0:\n",
    "            return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Took 1.093 seconds\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "from time import time\n",
    "numbers = [(1963309, 2265973), (2030677, 3814172),\n",
    "           (1551645, 2229620), (2039045, 2020802)]\n",
    "start = time()\n",
    "results = list(map(gcd, numbers))\n",
    "end = time()\n",
    "print('Took %.3f seconds' % (end - start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Took 0.982 seconds\n"
     ]
    }
   ],
   "source": [
    "# Example 3\n",
    "from concurrent.futures import ThreadPoolExecutor\n",
    "\n",
    "start = time()\n",
    "pool = ThreadPoolExecutor(max_workers=2)\n",
    "results = list(pool.map(gcd, numbers))\n",
    "end = time()\n",
    "print('Took %.3f seconds' % (end - start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Took 0.565 seconds\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "from concurrent.futures import ProcessPoolExecutor\n",
    "\n",
    "start = time()\n",
    "pool = ProcessPoolExecutor(max_workers=2)  # The one change\n",
    "results = list(pool.map(gcd, numbers))\n",
    "end = time()\n",
    "print('Took %.3f seconds' % (end - start))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
